<template>
  <view class="container">
    <view v-if="cart.length === 0" class="empty">
      当前购物车为空，请先添加商品
    </view>

    <view v-else>
      <view class="item" v-for="(item, index) in cart" :key="index">
        <view class="item-left">
          <image class="thumb" :src="item.image" mode="aspectFill" />
        </view>
        <view class="item-right">
          <view class="name">{{ item.name }}</view>
          <view class="count">数量：{{ item.count }}</view>
          <view class="price">￥{{ item.total }}</view>
        </view>
        <view class="delete">
          <button size="mini" type="warn" @click="remove(index)">删除</button>
        </view>
      </view>

      <view class="summary">
        总价：<text class="total">￥{{ totalPrice }}</text>
      </view>

      <button type="primary" @click="submit">提交订单</button>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'

const cart = ref([])

// 初始化假数据
onMounted(() => {
  cart.value = [
    {
      name: 'A4 彩色打印（双面）',
      count: 10,
      total: 5.0,
      image: 'https://img.yzcdn.cn/vant/cat.jpeg'
    },
    {
      name: '照片冲印 6寸',
      count: 3,
      total: 6.0,
      image: 'https://img.yzcdn.cn/vant/dog.jpeg'
    }
  ]
})

// 删除单项
const remove = (index) => {
  cart.value.splice(index, 1)
}

// 计算总价
const totalPrice = computed(() =>
  cart.value.reduce((sum, item) => sum + parseFloat(item.total), 0).toFixed(2)
)

// 模拟提交
const submit = () => {
  uni.showToast({ title: '提交成功', icon: 'success' })
  cart.value = []
}
</script>

<style scoped>
.container {
  padding: 30rpx;
}
.empty {
  text-align: center;
  color: #999;
  margin-top: 200rpx;
}
.item {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #eee;
  padding: 20rpx 0;
}
.item-left .thumb {
  width: 100rpx;
  height: 100rpx;
  border-radius: 10rpx;
}
.item-right {
  flex: 1;
  margin-left: 20rpx;
}
.name {
  font-weight: bold;
}
.count,
.price {
  color: #666;
  margin-top: 10rpx;
}
.delete {
  margin-left: 20rpx;
}
.summary {
  text-align: right;
  margin: 30rpx 0;
  font-size: 32rpx;
}
.total {
  color: red;
  font-weight: bold;
}
</style>